﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Elective1_kim_sequel
{
    public partial class Form_Main : Form
    {
        
        Form_Add form_Add;
        public Form_Main()
        {
            InitializeComponent();
            btn_view.Click += new EventHandler(btn_view_Click);
            btn_add.Click += new EventHandler(btn_add_Click);
            
            form_Add = new Form_Add();
            form_Add.FormClosed += new FormClosedEventHandler(form_Add_FormClosed);            
            
            this.Load += new EventHandler(Form_Main_Load);
            btn_delete.Click += new EventHandler(btn_delete_Click);
            btnExit.Click += new EventHandler(btnExit_Click);
            btn_Search.Click += new EventHandler(btn_Search_Click);
            textBoxSearch.KeyPress += new KeyPressEventHandler(textBoxSearch_KeyPress);
        }

        void textBoxSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            dataGridView.DataSource = Database.Table("SELECT * FROM tbl1 WHERE ID LIKE '%" + textBoxSearch.Text + "%' OR Last_Name LIKE '%" + textBoxSearch.Text + "%' OR First_Name LIKE '%" + textBoxSearch.Text + "%' OR Middle_Name LIKE '%" + textBoxSearch.Text + "%' OR User_Name LIKE '%" + textBoxSearch.Text + "%'");
        }


        void btn_Search_Click(object sender, EventArgs e)
        {
            if (textBoxSearch.Text.Trim() != "")
            {
                dataGridView.DataSource = Database.Table("SELECT * FROM tbl1 WHERE ID LIKE '%" + textBoxSearch.Text + "%' OR Last_Name LIKE '%" + textBoxSearch.Text + "%' OR First_Name LIKE '%" + textBoxSearch.Text + "%' OR Middle_Name LIKE '%" + textBoxSearch.Text + "%' OR User_Name LIKE '%" + textBoxSearch.Text + "%'");
            }
            
        }

        void Database_Query_Completed(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show("Record Deleted!");
            Database.Query_Completed -= new AsyncCompletedEventHandler(Database_Query_Completed);
            reloadDataGrid();
        }

        void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void btn_delete_Click(object sender, EventArgs e)
        {
            if(dataGridView.Rows.Count != 0)
            {
                DialogResult result = MessageBox.Show(" Are You Sure You Want to Delete This Record ?", " Conformation!", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (result == DialogResult.Yes)
                {
                    if (dataGridView.SelectedRows.Count == 1)
                    {
                        string key = dataGridView["ID", dataGridView.CurrentRow.Index].Value.ToString();
                        Database.Query_Completed += new AsyncCompletedEventHandler(Database_Query_Completed);
                        Database.Query_Async("DELETE FROM `mydatabase`.`tbl1` WHERE `tbl1`.`ID` = " + key);                      
                    }
                }
                else if (result == DialogResult.No)
                {
                    return;
                }
            }
        }

        void Form_Main_Load(object sender, EventArgs e)
        {
            if (Employee._AccessLevel == Employee.AccessLevel.User)
            {
                loaduser();
            }
        }

        
       
        

        void btn_add_Click(object sender, EventArgs e)
        {
            if (dataGridView.Rows.Count > 0)
            {
                form_Add.ShowDialog();
            }


           
        }

        void form_Add_FormClosed(object sender, FormClosedEventArgs e)
        {
            reloadDataGrid();
        }

        void btn_view_Click(object sender, EventArgs e)
        {
            reloadDataGrid();
        }

        void reloadDataGrid()
        {
            DataTable Table = Database.Table("SELECT * FROM tbl1");
            dataGridView.DataSource = Table;
        }

        void loaduser()
        {
            btn_add.Enabled = false;
            btn_delete.Enabled = false;
        }
    }
}
